from Box2D import b2Vec2
from pyglet.gl import *
from math import sin, cos

from constants import PIXELS_PER_METER

def world_to_pixels(x):
    """Convert world coordinate to pixel coordinate.

    Arguments:
    x -- the world coordinate to convert 

    Returns:
    The corresponding pixel coordinate.
    """
    return PIXELS_PER_METER * x

def pixels_to_world(x):
    """Convert pixel coordinate to screen coordinate.
    
    Arguments:
    x -- the pixel coordinate to convert


    Returns:
    The corresponding world coordinate.
    """
    return x/PIXELS_PER_METER

class Camera(object):
    def __init__(self, position, scale=1, angle=0):
        """__init__(self, position, scale, angle)
        
        Arguments:
        position - coordinate (x, y) which the camera is centered on
        scale - zoom factor
        angle - angle to rotate the camera"""
        
        self.x, self.y = position 
        self.angle = angle
        self.scale = scale

    def focus(self, win_width, win_height):
        """focus(self, win_width, win_height)
        
        Arguments:
        win_width - width of window
        win_height - height of window"""

        # Set up zoom
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        aspect = win_width / float(win_height)
        glOrtho(-self.scale * aspect * 200,
                 self.scale * aspect * 200,
                 -self.scale * 200,
                 self.scale * 200,
                 -10.0, 10.0)
        
        # Set up matrix to move, scale and rotate
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(self.x, self.y, +1.0, # Eye
                  self.x, self.y, -1.0, # Center
                  sin(self.angle), cos(self.angle), 0.0)

